package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketAdExample;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.model.admin.bo.ListBo;
import com.cskaoyan.model.admin.vo.AdBaseData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author jiaolei
 */
@Service
@Transactional
public class AdServiceImpl implements AdService {
    @Autowired
    MarketAdMapper marketAdMapper;
    /**
     * total : 4
     * pages : 1
     * limit : 20
     * page : 1
     * list : [{"id":4,"name":"广告标题改","link":"www.baidu1.com","url":"http://182.92.235.201:8083/wx/storage/fetch/5fwbas5vgf0qhuv3aft8.jpg","position":1,"content":"内容改1","enabled":false,"addTime":"2021-11-29 17:02:05","updateTime":"2021-11-29 17:10:53","deleted":false},{"id":1,"name":"合作 谁是你的菜","link":"","url":"http://yanxuan.nosdn.127.net/65091eebc48899298171c2eb6696fe27.jpg","position":1,"content":"合作 谁是你的菜","enabled":true,"addTime":"2018-02-01 00:00:00","updateTime":"2018-02-01 00:00:00","deleted":false},{"id":2,"name":"活动 美食节","link":"","url":"http://yanxuan.nosdn.127.net/bff2e49136fcef1fd829f5036e07f116.jpg","position":1,"content":"活动 美食节","enabled":true,"addTime":"2018-02-01 00:00:00","updateTime":"2018-02-01 00:00:00","deleted":false},{"id":3,"name":"活动 母亲节","link":"","url":"http://yanxuan.nosdn.127.net/8e50c65fda145e6dd1bf4fb7ee0fcecc.jpg","position":1,"content":"活动 母亲节5","enabled":true,"addTime":"2018-02-01 00:00:00","updateTime":"2018-02-01 00:00:00","deleted":false}]
     * List<MarketAd> list=
     */
    @Override
    public AdBaseData list(ListBo listBo) {
        PageHelper.startPage(listBo.getPage(), listBo.getLimit());
        MarketAdExample marketAdExample = new MarketAdExample();
        marketAdExample.setOrderByClause(listBo.getSort()+" "+ listBo.getOrder());
        List<MarketAd> marketAds = marketAdMapper.selectByExample(marketAdExample);

        PageInfo pageInfo = new PageInfo(marketAds);
        long total = pageInfo.getTotal();

        Integer pages=null;
        if(listBo.getLimit()!=null&& listBo.getLimit()!=0){
            pages=marketAds.size()/ listBo.getLimit()+1;
        }
        return AdBaseData.list(marketAds, listBo.getPage(), listBo.getLimit(),total,pages);

    }
    //{"name":"sdfasd","content":"sdfasd","url":"http://182.92.235.201:8083/wx/storage/fetch/9ofrn3f89ll3cosc3l31.JPG","link":"asdfa","position":1,"enabled":true}

    //{"errno":0,"data":{"id":9,"name":"sdfsdfg","link":"gsdfgdfgh","url":"http://182.92.235.201:8083/wx/storage/fetch/4f7ffvgjan05hp10thn4.JPG","position":1,"content":"gdfgdf","enabled":true,"addTime":"2021-11-29 21:31:55","updateTime":"2021-11-29 21:31:55"},"errmsg":"成功"}

    @Override
    public MarketAd create(MarketAd marketAd) {
        int i = marketAdMapper.insertSelective(marketAd);
        MarketAdExample marketAdExample = new MarketAdExample();
        marketAdExample.createCriteria().andNameEqualTo(marketAd.getName());
        List<MarketAd> marketAds = marketAdMapper.selectByExample(marketAdExample);
        return marketAds.get(0);
    }

    @Override
    public MarketAd update(MarketAd marketAd) {
        int i = marketAdMapper.updateByPrimaryKeySelective(marketAd);
        MarketAd marketAd1 = marketAdMapper.selectByPrimaryKey(marketAd.getId());
        return marketAd1;
    }

    @Override
    public MarketAd delete(MarketAd marketAd) {
        int i = marketAdMapper.deleteByPrimaryKey(marketAd.getId());
        return null;
    }
}
